package me.stone.stanimeclient.page.search

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.LayoutParams
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import me.stone.stanimeclient.core.common.extensions.dip
import me.stone.stanimeclient.databinding.HolderSearchHistoryItemBinding
import kotlin.math.roundToInt

class SuggestionTextAdapter(
    private val doQueryEvent: IDoQueryEvent,
    private val doDeleteSuggestionEvent: IDoDeleteSuggestionEvent,
    private val doClearHistoryEvent: IClearSearchHistoryEvent
) : PagingDataAdapter<String, SuggestionTextAdapter.SuggestionTextViewHolder>(
    diffCallback = object : DiffUtil.ItemCallback<String>() {
        override fun areItemsTheSame(oldItem: String, newItem: String): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: String, newItem: String): Boolean {
            return oldItem == newItem
        }
    }
) {

    private class FooterViewHolder(textView: TextView): ViewHolder(textView)

    private class FooterAdapter (
        private val doClearHistoryEvent: IClearSearchHistoryEvent
    ): LoadStateAdapter<FooterViewHolder>() {

        override fun onBindViewHolder(holder: FooterViewHolder, loadState: LoadState) {}

        override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): FooterViewHolder {
            return FooterViewHolder(
                TextView(parent.context).apply {
                    layoutParams = ConstraintLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT
                    )
                    val dp8 = 8f.dip.roundToInt()
                    setPaddingRelative(dp8, dp8, dp8, dp8)
                    textSize = 16f
                    setTextColor(Color.RED)
                    textAlignment = View.TEXT_ALIGNMENT_CENTER
                    setText(me.stone.stanimeclient.core.ui.R.string.delete_all_search_history)
                    setOnClickListener {
                        doClearHistoryEvent.doClearHistory()
                    }
                }
            )
        }

        override fun displayLoadStateAsItem(loadState: LoadState): Boolean {
            return loadState is LoadState.NotLoading
        }

    }

    var query: String = ""
        set(value) {
            field = value
            notifyItemRangeChanged(0, itemCount)
        }

    inner class SuggestionTextViewHolder(
        val binding: HolderSearchHistoryItemBinding
    ) : ViewHolder(binding.root)

    override fun onBindViewHolder(holder: SuggestionTextViewHolder, position: Int) {
        val suggestion = getItem(position) ?: return
        holder.binding.run {
            this.searchContent = query
            this.suggestion = suggestion
            executePendingBindings()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SuggestionTextViewHolder {
        return SuggestionTextViewHolder(
            HolderSearchHistoryItemBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            ).apply {
                doQueryEvent = this@SuggestionTextAdapter.doQueryEvent
                doDeleteSuggestionEvent = this@SuggestionTextAdapter.doDeleteSuggestionEvent
            }
        )
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        recyclerView.layoutManager = LinearLayoutManager(recyclerView.context).apply {
            orientation = LinearLayoutManager.VERTICAL
        }
        if (recyclerView.itemDecorationCount == 0) {
            recyclerView.addItemDecoration(
                DividerItemDecoration(
                    recyclerView.context, DividerItemDecoration.VERTICAL
                )
            )
        }
        if (recyclerView.paddingStart == 0 || recyclerView.paddingTop == 0) {
            val dp8 = 8f.dip.roundToInt()
            recyclerView.setPaddingRelative(dp8, dp8 / 2, dp8, dp8 / 2)
        }
    }

    private val footerAdapter by lazy {
        FooterAdapter(doClearHistoryEvent)
    }

    fun withFooterAdapter() = withLoadStateFooter(footerAdapter)

}